/*
 * ContactListCellRenderer.java
 *
 * Created on 7 novembre 2007, 13:54
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package jtalk.view;

import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

import jtalk.controller.Properties;
import jtalk.model.Contact;
import jtalk.view.ressources.StatusIcons;

/**
 * 
 * @author remy
 */
public class ContactListCellRenderer extends DefaultTreeCellRenderer {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 1879867084212242772L;
	
	
	/** Creates a new instance of ContactListCellRenderer */
    public ContactListCellRenderer() {
    	//Set defaults icons
        this.setOpenIcon(StatusIcons.ICON_EXPAND);
        this.setClosedIcon(StatusIcons.ICON_COLLAPSE);        
    }
    
    public Component getTreeCellRendererComponent(
            JTree tree,
            Object value,
            boolean sel,
            boolean expanded,
            boolean leaf,
            int row,
            boolean hasFocus) {

    			super.getTreeCellRendererComponent(
    					tree, value, sel,
    					expanded, leaf, row,
    					hasFocus);
    			if (leaf) {
    				setIcon(getStatusIcon(value));    				
    			}
    			return this;
    }

    protected ImageIcon getStatusIcon(Object value) {
        
        
        try{
        	
        	DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
        	
        	Contact contact = (Contact)(node.getUserObject());
        	
        	if(contact.isAvailable()) {
        		if(contact.getStatus().equals(Properties.STATUS_AWAY.value())) {
        			return StatusIcons.ICON_AWAY;
        		}else if(contact.getStatus().equals(Properties.STATUS_BUSY.value())) {        			
        			return StatusIcons.ICON_BUSY;
        		}else {
        			return StatusIcons.ICON_AVAILABLE;
        		}        			        		
        	}else {
        		return StatusIcons.ICON_UNAVAILABLE;
        	}
        	
        }catch(ClassCastException e){}
        
        return new ImageIcon();
        
    }
	
    
}
